import Vue from "vue";
import dayjs from "dayjs";
import emoji_convert from "../utils/emoji_convert";
import { format, transformTime, transformUserInfoTime } from "../utils/format_date";
import { numberConvert } from "../utils/round_number";

const filter = {
	// 实现一年前、几个月前、刚刚功能
	transformTime: (value, formatStr = "YYYY/MM/DD HH:mm:ss") => {
		const format = dayjs(+value).format(formatStr);
		return transformTime(format);
	},
	// 根据时间戳转换为多少天
	transformUserInfoTime: (value, formatStr = "YYYY/MM/DD HH:mm:ss") => {
		let regex = /\d{13}/;
		if (!regex.test(value)) return value;
		// 提取出时间戳将其转换
		let time = regex.exec(value)[0];
		const format = dayjs(+time).format(formatStr);
		let result = transformUserInfoTime(format);
		return value.replace(regex, result);
	},
	// 字符串转成 emoji
	uncodeUtf16: value => {
		if (!value) return value;
		return emoji_convert.uncodeUtf16(value);
	},
	// 格式化时间 2020-09-26
	format,
	// 格式化数值，进行千、万单位取整
	numberConvert,
};

for (let key in filter) {
	// new Vue before
	Vue.filter(key, filter[key]);
}
